/**

  **************************************************************************
  * @file     main.c
  * @version  v2.1.2
  * @date     2022-08-16
  * @brief    main program
  **************************************************************************
  *                       Copyright notice & Disclaimer
  *
  * The software Board Support Package (BSP) that is made available to
  * download from Artery official website is the copyrighted work of Artery.
  * Artery authorizes customers to use, copy, and distribute the BSP
  * software and its related documentation for the purpose of design and
  * development in conjunction with Artery microcontrollers. Use of the
  * software is governed by this copyright notice and the following disclaimer.
  *
  * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
  * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
  * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
  * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
  * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
  *
  **************************************************************************
  */
#define YXL_MAIN_TEST
#ifndef YXL_MAIN_TEST
#include "AT.h"
#include "at32f403a_407_gpio.h"
#include "mcu.h"
#include "Wireless_Tcp.h"
#include "app_compiler.h"

// #define UART_TEST
#define DMA_TEST

uint8_t yiyuan_pwr = 0;;
extern bool Scene_Activiate_State;

/*
* 描述: 用于移远EC800M模组开关机,此函数不做调用
* 输入:
* 输出:
*/
static void EC800M_PWRKEY(void)
{
   gpio_init_type gpio_instruct;
   
   crm_periph_clock_enable(CRM_GPIOD_PERIPH_CLOCK,TRUE);      
   
   gpio_instruct.gpio_drive_strength =GPIO_DRIVE_STRENGTH_STRONGER ;
   gpio_instruct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL ;
   gpio_instruct.gpio_pull = GPIO_PULL_NONE;

   gpio_instruct.gpio_mode = GPIO_MODE_OUTPUT;
   gpio_instruct.gpio_pins = GPIO_PINS_13 | GPIO_PINS_15 ;    
   gpio_init(GPIOD,&gpio_instruct); 

   
   gpio_bits_set(GPIOD,GPIO_PINS_15);
//   delay_ms(300);
//   gpio_bits_set(GPIOD,GPIO_PINS_13);
   delay_ms(800);
   gpio_bits_set(GPIOD,GPIO_PINS_13);
//   gpio_bits_reset(GPIOD,GPIO_PINS_13);
}

void QUECTEL_PWR(void)
{
    uint8_t Sendbuff[3] = {'A', 'T', 0X0D};
    
   USART_Send(UART_AT,Sendbuff,3);
   delay_ms(20);
   yiyuan_pwr = usart_data_receive(UART4);
   
   if(yiyuan_pwr == NULL)
   {
      EC800M_PWRKEY();
   }

   delay_ms(3000);
}

static void QUECTEL_4G_WAKEPIN(void)
{
   gpio_init_type gpio_instruct;
   
   crm_periph_clock_enable(CRM_GPIOD_PERIPH_CLOCK,TRUE);      
   
   gpio_instruct.gpio_drive_strength =GPIO_DRIVE_STRENGTH_STRONGER ;
   gpio_instruct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL ;
   gpio_instruct.gpio_pull = GPIO_PULL_NONE;

   gpio_instruct.gpio_mode = GPIO_MODE_OUTPUT;
   gpio_instruct.gpio_pins = GPIO_PINS_3;    
   gpio_init(GPIOD,&gpio_instruct); 
   
   gpio_bits_set(GPIOD,GPIO_PINS_3);
   
   delay_ms(200);
   
   gpio_bits_reset(GPIOD,GPIO_PINS_3);
}


ATStatus login_test1(ReSetState reset, char* str)
{
   switch (reset)
   {
   case RE_SUCCESS:
      LOG_DBG("recv msg success , %s\n", str);
      ATCommandRegister(AT, EXEXCMD, "AT+CGMM\r", 8, NULL);
      break;
   default:
      LOG_ERR("recv msg failed, state is %d ,recv is %s\n", reset, str);
      break;
   }

   return ATSUCCESS;
}

ATStatus login_test(ReSetState reset, char* str)
{
   switch (reset)
   {
   case RE_SUCCESS:
        LOG_DBG("recv msg success , %s \n", str);
      //LOG_DBG("recv msg success , %s %d\n", str, strlen(str));
      // ATCommandRegister(AT, EXEXCMD, "AT\r", NULL, login_test1);
   	// ATCommandRegister(AT, EXEXCMD, "AT+QCCID\r", NULL, login_test1);
      ATCommandRegister(AT, EXEXCMD, "AT+CGMM\r", 8, NULL);
   	// ATCommandRegister(AT, EXEXCMD, "AT+QHTTPURL=10\r", NULL, login_test1);
      break;
   default:
      LOG_ERR("recv msg failed, state is %d ,recv is %s\n", reset, str);
      break;
   }

   return ATSUCCESS;
}
static void uart_test_func(void)
{
	int ret = 0;
   uint8_t str[6] = {'s','t','a','r','t', 0X0D};
   uint8_t str1[8] = {'A','T','+','C','G','M','M',0X0D};
   uint8_t Sendbuff[3] = {'A', 'T', 0X0D};
   char *bin = "AT";
   uint16_t hex[64] = {0}, i=0;
   bool state = false;
//  Uart_Init_Channel(UART_PRINTF, BAUDRATE115200);
//  Uart_Init_Channel(UART_AT, BAUDRATE115200);
   
//   
   QUECTEL_4G_WAKEPIN();
   if(ATFormInit() != ATSUCCESS)
   {
      LOG_ERR("Erorr ATInit\n");
   }
   //delay_ms(1000);
  // ATCommandRegister(AT, EXEXCMD, "AT+CSQ\r", NULL, login_test);
   // ATCommandRegister(AT, EXEXCMD, "AT+CGMM\r", NULL, login_test);
	// ATCommandRegister(AT, EXEXCMD, "AT+QCCID\r", NULL, login_test);
   //ATCommandRegister(AT, EXEXCMD, "AT+QNWINFO\r", NULL, login_test);
   // ATCommandRegister(AT, EXEXCMD, "AT+QLTS\r", NULL, login_test);
   Wireless_Scene_Config(1, 1, "UNINET", NULL, NULL);
   // Wireless_Scene_Dis_Activiate(1);

   while(1)
   {
      // delay_ms(100);
      ret = AT_MainFunction();
      if(state == false && Scene_Activiate_State == true)
      {
         state = true;
         Wireless_Tcp_client_buffer_mode(1, 0, "TCP", "122.114.122.174", 42859, 1);
      }

		// LOG_DBG("function  state = %d\n", ret);
   }
}

extern void usart1_dma_init(u32 bound);
extern void usartdmarecv(u8 *data, u16 len);
extern void usartdmasend(u8 *data, u16 len);

int main_Test(void)
{
   uint8_t str[] = "AT\r";
   // 公有部分
   system_clock_config();
   nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
   delay_init();
   
#ifdef UART_TEST
   uart_test_func();
#endif

   // Usart_Dma();
//   usart1_dma_init(115200);
    Uart_Init_Channel(UART_PRINTF, BAUDRATE115200);
   uart4_init();
   dma_uart4_init();
   QUECTEL_PWR();
   
   uart_test_func();
   while(1) ;
   return 0;
}
#endif



